#include "pagetractionblock.h"
#include "ui_pagetractionblock.h"

PageTractionBlock::PageTractionBlock(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PageTractionBlock)
{
    ui->setupUi(this);
    setAutoFillBackground(true);  //设置自动填充
    QPalette palette = BKGCLR;
    setPalette(palette);

   m_lTBTrigReasons << " 零速时门未关好牵引封锁"
                     << "停放制动未缓解牵引封锁"
                     << "制动指令列车线激活牵引封锁"
                     << "紧急制动牵引封锁"
                     << "快速制动牵引封锁"
                     << "牵引指令列车线异常牵引封锁"
                     << "保持制动缓解故障牵引封锁"
                     << "占有端人工驾驶模式下司控器级位信号无效牵引封锁"
                     << "列车触发超速保护牵引封锁"
                     << "6个以上架气制动丢失牵引封锁"
                     << "3个及以上牵引故障牵引封锁";
    InitTBTrigLabels();
}

PageTractionBlock::~PageTractionBlock()
{
    delete ui;
}

void PageTractionBlock::InitTBTrigLabels()
{
    m_nTopSpace = 110;
    m_nBottomSpace = 50;
    m_nHSpace = 50;
    m_nLabelLen = 600;
    m_nLabelHeight = 50;
    m_nVSpace = 10;

    int Cnt = m_lTBTrigReasons.length();
    uint cols = Cnt%ROWSINCOL_TB?(Cnt/ROWSINCOL_TB+1):(Cnt/ROWSINCOL_TB);
    m_nSideSpace = (this->width() - cols*m_nLabelLen-(cols-1)*m_nHSpace)/2;
    m_nStartx = m_nSideSpace;
    m_nStarty = m_nTopSpace;

    for(int i=0;i<Cnt;i++)
    {
        uint nCol = i/ROWSINCOL_TB;
        QLabel *ebtrig = new QLabel(this);
        ebtrig->setStyleSheet(StatLabelStyle_TB);
        ebtrig->setGeometry(m_nStartx+nCol*(m_nLabelLen+m_nHSpace),
                           m_nStarty+(i%ROWSINCOL_TB)*(m_nLabelHeight+m_nVSpace),
                           m_nLabelLen,m_nLabelHeight);
        ebtrig->setAlignment(Qt::AlignCenter);
        ebtrig->setText(m_lTBTrigReasons.at(i));
        m_lTBTrigLabels.append(ebtrig);
        m_lTBRigStats.append(0);
    }
}

void PageTractionBlock::setTBTrigStat(int index, int stat)
{
    if(index >= m_lTBTrigReasons.length())
        return;

    switch (stat) {
    case 0:
        m_lTBTrigLabels[index]->setStyleSheet(StatLabelStyle_TB);
        break;
    case 1:
        m_lTBTrigLabels[index]->setStyleSheet(LabelRedStyle_TB);
        break;
    default:
        m_lTBTrigLabels[index]->setStyleSheet(StatLabelStyle_TB);
        break;
    }

    m_lTBRigStats[index] = stat;
    if(m_lTBRigStats.contains(1))
        emit signal_one_tbtrig_act(true);
    else
        emit signal_one_tbtrig_act(false);
}

void PageTractionBlock::on_Btn_Return_clicked()
{
    emit signal_go_back();
}
